package test.enba.es.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import test.enba.es.doc.Product;
import test.enba.es.service.ProductService;

@RestController
@RequestMapping("/products")
public class ProductController {

  @Autowired private ProductService productService;

  // 创建产品
  @PostMapping
  public Product createProduct(@RequestBody Product product) {
    return productService.saveProduct(product);
  }

  // 根据ID获取产品
  @GetMapping("/{id}")
  public Product getProductById(@PathVariable String id) {
    return productService.findProductById(id);
  }

  // 根据名称获取产品
  @GetMapping("/search")
  public List<Product> getProductsByName(@RequestParam String name) {
    return productService.findProductsByName(name);
  }

  // 根据价格范围获取产品
  @GetMapping("/price-range")
  public List<Product> getProductsByPriceRange(
      @RequestParam double minPrice, @RequestParam double maxPrice) {
    return productService.findProductsByPriceRange(minPrice, maxPrice);
  }

  // 删除产品
  @DeleteMapping("/{id}")
  public void deleteProduct(@PathVariable String id) {
    productService.deleteProduct(id);
  }

  // 分页查询产品
  @GetMapping("/page-search")
  public Page<Product> searchProductsByName(
      @RequestParam String name,
      @RequestParam(defaultValue = "0") int page,
      @RequestParam(defaultValue = "10") int size) {
    return productService.searchProductsByName(name, page, size);
  }
}
